package engine;

import statistics.WriteStatistics;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * This class contain information about all cells and creatures
 */
public class World {
    private static Logger log = Logger.getLogger(World.class.getName());

    public static final int DAYS_IN_YEAR = 365;
    public final Cell[] field;
    public final List<Creature> creaturesOfWorld;
    public final int fieldWidth;
    private int yearNumber = 1;
    private int dayNumber = 1;
    public StringBuilder shLog;

    public World(int length, int width) {
        field = new Cell[length * width];
        fieldWidth = width;
        shLog = new StringBuilder("");
        Color color = Color.GREEN;
        for (int x = 0; x < length; x++) {
            for (int y = 0; y < width; y++) {
                field[x * width + y] = new Cell(x, y, color, this);
            }
        }
        creaturesOfWorld = new ArrayList<>();
        //cellsOfWorld = new ArrayList<>();
        System.out.println("New world with " + length + "x" + width + " size was created");
    }

    public void worldDay() {
        for (Cell currentCell : field) {
            currentCell.cellDay();
        }
        WriteStatistics.writeToFile(this);
        shLog.append("\nDay №").append(dayNumber).append(" is over.");
        log.info("Day №" + dayNumber + " is over.-----------------------------");
        setDayNumber(dayNumber + 1);
    }

    public void setYearNumber(int yearNumber) {
        this.yearNumber = yearNumber;
    }

    public void setDayNumber(int dayNumber) {
        if (dayNumber <= DAYS_IN_YEAR) {
            this.dayNumber = dayNumber;
        } else {
            setYearNumber(yearNumber + 1);
            this.dayNumber = dayNumber % DAYS_IN_YEAR;
        }
    }

    public int getYearNumber() {
        return yearNumber;
    }

    public int getDayNumber() {
        return dayNumber;
    }
}
